package com.itheima.d3_tcp.no3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;

/**
 * 服务端线程任务类，负责：处理Socket，接收客户端发送过来的数据！
 */
public class ServerRunnable implements Runnable {

    Socket socket;

    public ServerRunnable(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        //任务：接收数据，获取输入流

        try {
            InputStream is = socket.getInputStream();
            //把普通字节流包装成缓冲字符输入流，一次读取一行
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(socket.getRemoteSocketAddress() + " 客户端发送的消息：" + line);
            }
            System.out.println("客户端正常退出！");
            socket.close();
        } catch (IOException e) {
            System.out.println("客户端异常退出！");
        }
    }
}
